<!--
 * @Descripttion: 
 * @version:  手写第二遍
 * @Author: Zuo
 * @Date: 2021-07-30 15:26:01
 * @LastEditors: Zuo
 * @LastEditTime: 2021-08-12 10:33:45
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="./lib/promiseJs2.js"></script>
    <!-- <script>
      const p = new Promise((resolve,reject)=>{
           setTimeout(()=>{
               reject(1);
               console.log('我执行后');
           },100)
       }).then(
           value =>{
               console.log('onResolved1',value);
           },
           reason =>{
                console.log('onRejected1',reason);
               // return 3
               // throw 4
                return  new Promise((resolve,reject)=>{
                    reject(5)
                })
           }
       ).then(
           value =>{
               console.log('onResolved2',value);
           },
        //    reason =>{
        //         console.log('onRejected2',reason);
        //    }
       ).catch(
           reason =>{
            console.log('onRejected3 + catch',reason);
            return new Promise(()=>{});  //  将promise 变成pending 就可以中断promise 链。
           }
       ).then(
           value =>{
               console.log('onResolved4',value);
           },
           reason =>{
                console.log('onRejected4',reason);
           }
       )
    </script> -->

    <script>
        // resolve 用法几种情况
        const p1 = Promise.resolve(2);  // 如果接受一般值， promise成功， value就是这个值
        const p2 = Promise.resolve(Promise.resolve(3));  // 如果接受成功的promise， promise成功， value就是这个promise的value值
        const p3 = Promise.resolve(Promise.resolve(4)); // 如果接受成功的promise， promise失败， reason就是这个promise的reason值
        // p1.then(value => {console.log('p1',value)})
        // p2.then(value => {console.log('p2',value)})
        // p3.catch(value => {console.log('p3',value)})
        // Promise.all([p1,p2,p3]).then(
        //     value =>{
        //         console.log('value' , value)
        //     },
        //     reason =>{
        //         console.log('reason', reason)
        //     }
        // )


        Promise.race([p1,p2,p3]).then(
            value =>{
                console.log('value' , value)
            },
            reason =>{
                console.log('reason', reason)
            }
        )
    </script>
    
</body>
</html>